package com.liaohuakai.piggybank.base

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDialog
import androidx.appcompat.widget.AppCompatTextView
import com.liaohuakai.piggybank.R

/**
 * Author: Leo Liao <lliao@midtronics.com>
 * Date: 16/07/2025 17:26
 * Description:
 */
abstract class BaseActivity : AppCompatActivity() {
    private var dialog: AppCompatDialog? = null

    fun showLoading(msg: String? = null) {
        dialog?.let {
            if (!it.isShowing) {
                it.show()
            }
            it.findViewById<AppCompatTextView>(R.id.layout_loading_dialog_tv_msg)?.text = msg
            return
        }
        dialog = AppCompatDialog(this).apply {
            requestWindowFeature(Window.FEATURE_NO_TITLE)
            setContentView(R.layout.layout_loading_dialog)
            setCancelable(false)

            window?.apply {
                setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                setDimAmount(0.5f) // 背景变暗程度（0-1） }
            }

            msg?.let {
                findViewById<AppCompatTextView>(R.id.layout_loading_dialog_tv_msg)?.text = it
            }

            show()
        }

    }

    fun hideLoading() {
        dialog?.dismiss()
        dialog = null
    }
}